//
//  SwitchViewController.m
//  View Switcher
//
//  Created by Haili Ma on 11/11/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "SwitchViewController.h"
#import "BlueViewController.h"
#import	"YellowViewController.h"

@implementation SwitchViewController

@synthesize blueViewController;
@synthesize yellowViewController;

/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

- (IBAction) switchViews: (id) sender {
	if (self.yellowViewController == nil) {
		YellowViewController *yellowController = [[YellowViewController alloc] initWithNibName:@"YellowViewController"  bundle:nil];
		self.yellowViewController = yellowController;
		[yellowController release];
	}
	
//	if (self.blueViewController.view.superview == nil) {
//		[yellowViewController.view removeFromSuperview];
//		[self.view insertSubview:blueViewController.view	atIndex:0];
//	}
//	else {
//		[blueViewController.view removeFromSuperview];
//		[self.view insertSubview:yellowViewController.view	atIndex:0];
//	}

	[UIView beginAnimations: @"View Flip" context:nil];
	[UIView setAnimationDuration:1.25];
	[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
	
	
	if (self.blueViewController.view.superview == nil) {
		[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];
		[blueViewController viewWillAppear:YES];
		[yellowViewController viewWillDisappear:YES];
		[yellowViewController.view removeFromSuperview];
		[self.view insertSubview:blueViewController.view	atIndex:0];
		[yellowViewController viewDidDisappear:YES];
		[blueViewController viewDidDisappear:YES];
	}
	else {
		[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];
		[yellowViewController viewWillAppear:YES];
		[blueViewController viewWillDisappear:YES];
		[blueViewController.view removeFromSuperview];
		[self.view insertSubview:yellowViewController.view	atIndex:0];
		[blueViewController viewDidDisappear:YES];
		[yellowViewController viewDidDisappear:YES];

	}
	
	[UIView commitAnimations];
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
	BlueViewController *blueController = [[BlueViewController alloc] initWithNibName:@"BlueViewController" bundle:nil];
	self.blueViewController = blueController;
	[self.view insertSubview:[blueController view] atIndex:0];
	[blueController release];
    [super viewDidLoad];
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
	[yellowViewController release];
	[blueViewController release];
    [super dealloc];
}


@end
